package com.wt.controller;

import com.wt.pojo.Distributor;
import com.wt.pojo.WithdrawOrder;
import com.wt.service.DistributorService;
import com.wt.service.WithDrawService;
import com.wt.util.Pager;
import com.wt.util.Pagination;
import com.wt.util.PropertiesLoad;
import com.wt.vo.DistributorIncome;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Author hiberk
 * Date 2017/6/28 9:52
 * 分销商Controller
 */
@Controller
@RequestMapping("/distributor")
public class DistributorController {
    @Resource
    private DistributorService distributorService;
    @Resource
    private WithDrawService withDrawService;

    @RequestMapping(value = "/details")
    public String details() throws Exception {

        return "/distributor/details";
    }

    @RequestMapping(value = "/details_frag")
    public String details_frag(Integer id, String start_time, String end_time, Model model, Integer page, Integer pageSize, HttpServletRequest request) throws Exception {
        id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
        if (!start_time.equals("") && !end_time.equals("")) {
            try {
                start = format.parse(start_time + " 00:00:01");
                end = format.parse(end_time + " 23:59:59");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime() / 1000; //获得的是ms 这里需要秒
            timeEnd = end.getTime() / 1000;
        }
        Pager pager = new Pager();
        pager.setPageth(page == null ? 1 : page);
        pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);
        Pagination<DistributorIncome> pagination = distributorService.selectDistributorInCome(id, timeStart, timeEnd, pager);
        model.addAttribute("pagination", pagination);
        return "/distributor/details_frag";
    }

    @RequestMapping(value = "/withdraw")
    public String withdraw(HttpServletRequest request, Model model) {
        Integer id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
        Distributor distributor = distributorService.getDistributorById(id);
        BigDecimal accountTotal = withDrawService.selectAccountTotal(id);
        BigDecimal tradeTotal = withDrawService.selectTradeTotal(id);
        BigDecimal commissionTotal = new BigDecimal(0);
        if (tradeTotal != null) {
            commissionTotal = tradeTotal.multiply(distributor.getProportion());
        }


        model.addAttribute("distributor", distributor);
        model.addAttribute("accountTotal", accountTotal == null ? 0 : accountTotal);
        model.addAttribute("tradeTotal", tradeTotal == null ? 0 : tradeTotal);
        model.addAttribute("commissionTotal", commissionTotal == null ? 0 : commissionTotal);
        return "/distributor/withdraw";
    }

    @RequestMapping(value = "/withdraw_frag")
    public String withdraw_frag(Integer id, String start_time, String end_time, Model model, Integer page, Integer pageSize, HttpServletRequest request) throws Exception {
        id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
        if (!start_time.equals("") && !end_time.equals("")) {
            try {
                start = format.parse(start_time + " 00:00:01");
                end = format.parse(end_time + " 23:59:59");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime() / 1000;
            timeEnd = end.getTime() / 1000;
        }
        Pager pager = new Pager();
        pager.setPageth(page == null ? 1 : page);
        pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);
        Pagination<WithdrawOrder> pagination = withDrawService.selectWithDrawOrders(id, timeStart, timeEnd, pager);
        model.addAttribute("pagination", pagination);
        return "/distributor/withdraw_frag";
    }

    @RequestMapping(value = "/withdraw_invoke", method = RequestMethod.POST)
    @ResponseBody
    public boolean withdraw_invoke(WithdrawOrder order, HttpServletRequest request) {
        try {
            Integer id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
            Long withdrawId = 100 * id + System.currentTimeMillis();
            order.setWithdrawId(withdrawId);
            order.setDistributorIds(id);
            Long time = (new Date().getTime() / 1000);
            order.setSendTime(time);
            return withDrawService.saveWithdrawOrder(order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequestMapping(value = "/check_withdraw", method = RequestMethod.POST)
    @ResponseBody
    public boolean check_withdraw(HttpServletRequest request) {
        try {
            Integer id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
            return withDrawService.selectCheckStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequestMapping(value = "/admin")
    public String admin(HttpServletRequest request, Model model) {
        Integer id = ((Distributor) request.getSession().getAttribute("distributor")).getId();
        Distributor distributor = distributorService.getDistributorById(id);
        BigDecimal accountTotal = withDrawService.selectAccountAll();
        BigDecimal tradeTotal = withDrawService.selectTradeAll();
        BigDecimal commissionTotal = withDrawService.selectTradeCommission();
        Long orderCount = distributorService.selectCountOrders();
        model.addAttribute("orderCount", orderCount);
        model.addAttribute("accountTotal", accountTotal);
        model.addAttribute("tradeTotal", tradeTotal);
        model.addAttribute("commissionTotal", commissionTotal);
        return "/distributor/count";
    }

    @RequestMapping(value = "/admin_frag")
    public String admin_frag(String start_time, String end_time, Model model, Integer page, Integer pageSize, HttpServletRequest request) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        Long timeStart = null;
        Long timeEnd = null;
        if (!start_time.equals("") && !end_time.equals("")) {
            try {
                start = format.parse(start_time + " 00:00:01");
                end = format.parse(end_time + " 23:59:59");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            timeStart = start.getTime() / 1000; //获得的是ms 这里需要秒
            timeEnd = end.getTime() / 1000;
        }
        Pager pager = new Pager();
        pager.setPageth(page == null ? 1 : page);
        pager.setPageSize(pageSize == null ? Pager.DEFAULT_DISPLAY_PAGETHS_COUNT : pageSize);
        Pagination<DistributorIncome> pagination = distributorService.selectDistributorInCome(null, timeStart, timeEnd, pager);
        model.addAttribute("pagination", pagination);
        return "/distributor/count_frag";
    }


    @RequestMapping(value = "/promotion")
    public String promotion(HttpServletRequest request, Model model) {
        Integer id = ((Distributor) request.getSession().getAttribute("distributor")).getId();

        Properties prop =  PropertiesLoad.prop;

        String url = prop.getProperty("redirect.url") + "&fid=" + id;

        model.addAttribute("redirectUrl",url);
        return "/distributor/promotion";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request, Model model) {
        request.getSession().removeAttribute("distributor");
        return "redirect:/index";
    }

}
